package org.test.reflect;

import java.lang.annotation.Annotation;
import java.util.Arrays;

import org.junit.Test;
import org.springframework.stereotype.Repository;


/**
 * 两种方式得到类的Class对象
 * 
 * @author Administrator
 * 
 */

public class InitReflect {

	/**
	 * 通过路径名
	 * @throws ClassNotFoundException
	 */
//	@Test
	public void testClassForName() throws ClassNotFoundException {
		Class clazz = Class.forName("org.test.reflect.TargetClass", true,
				this.getClass().getClassLoader());
		
		java.lang.reflect.Method[] methods = clazz.getDeclaredMethods();//得到公开methods数组
		for (java.lang.reflect.Method method : methods) {
			
			System.out.println("方法有"+method.getName());
		}
	}

	/**
	 * 通过已有对象建
	 */
//	@Test
	public void testGetClass() {
		TargetClass targetClass = new TargetClass();
		Class targetClass2 = targetClass.getClass();

		java.lang.reflect.Method[] methods = targetClass2.getMethods();
		for (java.lang.reflect.Method method : methods) {
			System.out.println("方法有："+method.getName());
		}
	}
	
	
	
	/**
	 * 通过路径名
	 * @throws ClassNotFoundException
	 */
	@Test
	public void testReflectAPI() throws ClassNotFoundException {
		
		String s = "org.test.dao.HibernateBaseDao";
		Class clazz = Class.forName(s, true,
				this.getClass().getClassLoader());
		
		java.lang.reflect.Method[] methods = clazz.getDeclaredMethods();//得到公开methods数组
		for (java.lang.reflect.Method method : methods) {
			System.out.println("得到的公共的方法有："+method.getName()+"其annotation有："+Arrays.toString(method.getAnnotations()));
			
		}
		
		
		Annotation[] annotations = (Annotation[]) clazz.getAnnotations();
		Annotation annotation = clazz.getAnnotation(Repository.class	);
		if(annotation!=null){
			System.out.println("存在Repository这个annotation"+annotation);
		}
		for(Annotation a:annotations){
			System.out.println(" 得到的annotation有："+a);
		}
	}

}
